define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            var account =Config.one;
            var str1 = '';
            Fast.api.ajax({
                type: "POST",
                url: "user/user_layer/index",
                data: {account: account,one:0},
                async:false,
                dataType: "json",
            },function(data){
                var lists1 = data.child;
                if (lists1.length > 0) {
                    for(var i=0;i<lists1.length;i++) {
                        str1+=`
                                <li class="list">
                                  <i class="iconfont icon-sanjiaoxing hides"></i>
                                  <i class="iconfont icon-sanjiaoxing-copy shows"></i>
                                  <span class="textS" data-account="`+lists1[i].mobile+`">手机号:`+lists1[i].mobile+`,昵称:`+lists1[i].nickname+` ,直推人数:`+lists1[i].user_num+`, 间推人数:`+lists1[i].team_count+`</span>
                                  <ul class="treeS"></ul>
                                </li>`
                    }
                } else {
                    Toastr.success('没有下级了');

                }
                return false;
            })
            $('.treeS').html(str1);
            $('.hides').hide();
            $('.shows').show();

            $(document).on('click', ".textS", function(){
                if ($(this).next().children().length === 0) {
                    var account = $(this).data('account');
                    var str1 = '';
                    Fast.api.ajax({
                        type: "POST",
                        url: "user/user_layer/index",
                        data: {account: account},
                        async:false,
                        dataType: "json",
                    },function(data){
                        var lists1 = data.child;
                        if (lists1.length > 0) {
                            for(var i=0;i<lists1.length;i++) {
                                str1+=`
                                <li class="list">
                                  <i class="iconfont icon-sanjiaoxing hides"></i>
                                  <i class="iconfont icon-sanjiaoxing-copy shows"></i>
                                  <span class="textS" data-account="`+lists1[i].mobile+`">手机号:`+lists1[i].mobile+`,昵称:`+lists1[i].nickname+` ,直推人数:`+lists1[i].user_num+`, 间推人数:`+lists1[i].team_count+`</span>
                                  <ul class="treeS"></ul>
                                </li>`
                            }
                        } else {
                            Toastr.success('没有下级了');

                        }
                        return false;
                    })
                    $(this).siblings('.treeS').html(str1);
                    $(this).siblings('.hides').hide();
                    $(this).siblings('.shows').show();
                } else {
                    $(this).next().html('');
                    $(this).siblings('.hides').show();
                    $(this).siblings('.shows').hide();
                }
            })
            // /admin/tree/index/index   param:acount admin/member/Usertree/index
            function getData(account){
                Fast.api.ajax({
                    type: "POST",
                    url: "user/user_layer/index",
                    data: {account: account,one:1},
                    dataType: "json",
                    async:false,
                },function(data){
                    var lists = data.child;
                    var str = '';
                    if (lists.length > 0) {
                        for(var i=0;i<lists.length;i++) {
                            str+=`
                            <li class="list">
                              <i class="iconfont icon-sanjiaoxing hides"></i>
                              <i class="iconfont icon-sanjiaoxing-copy shows"></i>
                              <span class="textS" data-account="`+lists[i].mobile+`">手机号:`+lists[i].mobile+`, 昵称:`+lists[i].truename+`  , 直推人数:`+lists[i].user_num+`, 间推人数:`+lists[i].team_count+`</span>
                              <ul class="treeS"></ul>
                            </li>`
                        }
                        $('.treeS').html(str);
                    } else {
                        Toastr.info('没有下级了');
                    }
                    return false;

                })
            }
            $('.searchs').click(function () {
                var value = $('.res').val();
                // if(!(/^1[34578]\d{9}$/.test(value))){
                //     alert("请输入正确的手机号");
                //     return false;
                // }
                getData(value);
            })


            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user_layer/index' + location.search,
                    add_url: 'user/user_layer/add',
                    edit_url: 'user/user_layer/edit',
                    del_url: 'user/user_layer/del',
                    multi_url: 'user/user_layer/multi',
                    import_url: 'user/user_layer/import',
                    table: 'user_layer',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'pid', title: __('Pid')},
                        {field: 'level', title: __('Level')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
